package pl.plesken.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import pl.plesken.languages.SetLanguage;

public class EditorPlot extends Dialog {

	private Text titleText;
	private StyledText plotStyledText;
	protected Object result;
	protected Shell shell;
	String title;
	String part;
	String plot;
	
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getPart() {
		return part;
	}

	public void setPart(String part) {
		this.part = part;
	}

	public String getPlot() {
		return plot;
	}

	public void setPlot(String plot) {
		this.plot = plot;
	}

	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public EditorPlot(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public EditorPlot(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setLayout(new GridLayout());
		shell.setSize(650, 255);
		shell.setText(SetLanguage.getString("EditorPlot.AppName")); //$NON-NLS-1$

		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		composite.setLayout(new GridLayout());

		final Group describeGroup = new Group(composite, SWT.NONE);
		final GridData gd_describeGroup = new GridData(SWT.FILL, SWT.CENTER, true, false);
		describeGroup.setLayoutData(gd_describeGroup);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 3;
		describeGroup.setLayout(gridLayout);

		final Label partLabel = new Label(describeGroup, SWT.NONE);
		final GridData gd_partLabel = new GridData(SWT.FILL, SWT.CENTER, true, false);
		partLabel.setLayoutData(gd_partLabel);
		partLabel.setText(SetLanguage.getString("EditorPlot.part") + part); //$NON-NLS-1$

		final Label titleLabel = new Label(describeGroup, SWT.NONE);
		final GridData gd_titleLabel = new GridData(SWT.FILL, SWT.CENTER, false, false);
		gd_titleLabel.widthHint = 50;
		titleLabel.setLayoutData(gd_titleLabel);
		titleLabel.setText(SetLanguage.getString("EditorPlot.title")); //$NON-NLS-1$

		titleText = new Text(describeGroup, SWT.BORDER);
		final GridData gd_titleText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		titleText.setLayoutData(gd_titleText);
		titleText.setText(title);
		
		plotStyledText = new StyledText(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
		plotStyledText.setText(plot);
		final GridData gd_plotStyledText = new GridData(SWT.FILL, SWT.FILL, true, true);
		plotStyledText.setLayoutData(gd_plotStyledText);

		final Group manageGroup = new Group(composite, SWT.NONE);
		final GridData gd_manageGroup = new GridData(SWT.FILL, SWT.CENTER, true, false);
		manageGroup.setLayoutData(gd_manageGroup);
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 2;
		manageGroup.setLayout(gridLayout_1);

		final Button setButton = new Button(manageGroup, SWT.NONE);
		setButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				setIt();
			}
		});
		final GridData gd_setButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_setButton.widthHint = 100;
		setButton.setLayoutData(gd_setButton);
		setButton.setText(SetLanguage.getString("EditorPlot.set")); //$NON-NLS-1$

		final Button cancelButton = new Button(manageGroup, SWT.NONE);
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				shell.close();
			}
		});
		final GridData gd_cancelButton = new GridData(SWT.CENTER, SWT.CENTER, true, false);
		gd_cancelButton.widthHint = 100;
		cancelButton.setLayoutData(gd_cancelButton);
		cancelButton.setText(SetLanguage.getString("EditorPlot.cancel")); //$NON-NLS-1$
		//
	}

	private void setIt() {
		plot = plotStyledText.getText();
		title = titleText.getText();
		shell.close();
	}
}
